package com.hunan.api.catv.service.biz.tag_content;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.hunan.api.catv.common.ConstantsMsg;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.enums.ColumnEnum;
import com.hunan.api.catv.entity.po.TagContent;
import com.hunan.api.catv.entity.vo.TagContentVo;
import com.hunan.api.catv.entity.vo.TagVo;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.biz.tag.TagBiz;
import com.hunan.api.catv.service.impl.tag_content.TagContentService;
import com.hunan.api.catv.utils.GsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class TagContentBiz extends ResultService {

    @Autowired
    TagContentService tagContentService;

    @Autowired
    TagBiz tagBiz;

    @Autowired
    TagContentBiz tagContentBiz;

    public ServiceResult add(String tagIds, Integer contentId, Integer contentType, Integer columnId) {
        JsonObject tagObj = GsonUtils.fromJson(tagIds, JsonObject.class);
        JsonArray fir = GsonUtils.fromJson(GsonUtils.toJson(tagObj.get("fir")), JsonArray.class);
        boolean firFlag = tagContentBiz.batch(fir, 1, contentId, contentType, columnId);
        if (ColumnEnum.news.getCode().equals(contentType) && firFlag) {
            return buildOK(true, ConstantsMsg.admin_add_success);
        }
        if (ColumnEnum.video.getCode().equals(contentType) && firFlag) {
            JsonArray sec = GsonUtils.fromJson(GsonUtils.toJson(tagObj.get("sec")), JsonArray.class);
            boolean secFlag = tagContentBiz.batch(sec, 2, contentId, contentType, columnId);
            if (secFlag) {
                return buildOK(true, ConstantsMsg.admin_add_success);
            }
        }
        return buildFail(false, ConstantsMsg.admin_add_fail);
    }

    public ServiceResult edit(String tagIds, Integer contentId, Integer contentType, Integer columnId) {
        TagContent tagContent = new TagContent();
        tagContent.setContentId(contentId);
        tagContent.setContentType(contentType);
        tagContent.setColumnId(columnId);
        ServiceResult result = new ServiceResult();
        result = tagContentService.batchDelete(tagContent);
        if (result.getStatus()) {
            if (!ObjectUtils.isEmpty(tagIds)) {
                result = tagContentBiz.add(tagIds, contentId, contentType, columnId);
                if (result.getStatus()) {
                    return buildOK(true, ConstantsMsg.admin_edit_success);
                }
            }
            return buildOK(true, ConstantsMsg.admin_edit_success);
        }
        return buildFail(false, ConstantsMsg.admin_edit_fail);
    }

    public boolean batch(JsonArray ids, Integer level, Integer contentId, Integer contentType, Integer columnId) {
        boolean flag = true;
        for (int i = 0; i < ids.size(); i++) {
            TagContent tagContent = new TagContent();
            if (1 == level) {
                tagContent.setFirTagId(ids.get(i).getAsInt());
            }
            if (2 == level) {
                TagVo tagVo = tagBiz.getById(ids.get(i).getAsInt());
                tagContent.setFirTagId(tagVo.getParentId());
                tagContent.setSecTagId(ids.get(i).getAsInt());

            }
            tagContent.setColumnId(columnId);
            tagContent.setContentId(contentId);
            tagContent.setContentType(contentType);
            ServiceResult result = tagContentService.add(tagContent);
            if (!result.getStatus()) {
                flag = false;
                break;
            }
        }
        return flag;
    }

    public ServiceResult del(Integer tagId, Integer level) {
        TagContent tagContent = new TagContent();
        if (1 == level) {
            tagContent.setFirTagId(tagId);
        }
        if (2 == level) {
            tagContent.setSecTagId(tagId);
            ;
        }
        ServiceResult result = tagContentService.batchDelete(tagContent);
        if (result.getStatus()) {
            return buildOK(true, ConstantsMsg.admin_add_success);
        }
        return buildFail(false, ConstantsMsg.admin_add_fail);
    }

    public String getTagIds(Integer contentId, Integer contentType, Integer columnId) {
        JsonObject tagIds = new JsonObject();
        JsonArray fir = new JsonArray();
        JsonArray sec = new JsonArray();
        TagContent tagContent = new TagContent();
        tagContent.setContentId(contentId);
        tagContent.setContentType(contentType);
        tagContent.setColumnId(columnId);
        List<TagContentVo> list = tagContentService.list(tagContent);
        if (!ObjectUtils.isEmpty(list)) {
            for (TagContentVo tagContentVo : list) {
                if (tagContentVo.getSecTagId() == 0) {
                    fir.add(tagContentVo.getFirTagId());
                } else {
                    sec.add(tagContentVo.getSecTagId());
                }
            }
            tagIds.add("fir", fir);
            tagIds.add("sec", sec);
            return GsonUtils.toJson(tagIds);
        }
        return null;
    }
}
